package com.neusoft.databus.policy.repository.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.databus.policy.enums.PolicyExpType;
import com.neusoft.databus.policy.model.Expression;
import com.neusoft.databus.policy.model.QExpression;
import com.neusoft.databus.policy.repository.CustomizedExpressionRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

public class CustomizedExpressionRepositoryImpl extends AbstractRepository implements CustomizedExpressionRepository {

    @Override
    public Page<Expression> search(Pageable pageable, Searchable searchable) {
        QExpression qExpression = QExpression.expression;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(super.equalsEnum(qExpression.type, searchable, "type", value -> PolicyExpType.valueOf(value)));
        JPAQuery<Expression> query = this.query().selectFrom(qExpression).where(where);
        return super.search(query, pageable);
    }

    @Override
    public List<Expression> search(Searchable searchable) {
        QExpression qExpression = QExpression.expression;
        final BooleanBuilder where = new BooleanBuilder();
        where.and(super.equalsEnum(qExpression.type, searchable, "type", value -> PolicyExpType.valueOf(value)));
        JPAQuery<Expression> query = this.query().selectFrom(qExpression).where(where);
        return query.fetch();
    }

    @Override
    protected Class<?> getModelClass() {
        return Expression.class;
    }

}
